package View;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;

import Control.CtrlPaciente;
import Model.ItfOuvinte;
import Model.Paciente;

import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFormattedTextField;

public class Tpaciente extends JDialog implements ItfOuvinte {

	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JButton btnSalvar;
	private JButton btnFechar;
	private JTextField txtNome;
	private JComboBox<String> cmbSexo;
	private JComboBox<String> cmbStatus;
	private JFormattedTextField txtNasc;
	
	public Tpaciente() {
		
		setTitle("Cadastro de Pacientes");
		setBounds(100, 100, 369, 182);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		txtNome = new JTextField();
		txtNome.setColumns(10);
		txtNome.setBounds(61, 11, 294, 20);
		contentPanel.add(txtNome);
		
		JLabel lblNome = new JLabel("Nome");
		lblNome.setBounds(10, 14, 46, 14);
		contentPanel.add(lblNome);
		
		JLabel lblSexo = new JLabel("Sexo");
		lblSexo.setBounds(10, 43, 46, 14);
		contentPanel.add(lblSexo);
		
		cmbSexo = new JComboBox<String>();
		cmbSexo.setModel(new DefaultComboBoxModel(new String[] {"Masculino", "Feminino"}));
		cmbSexo.setBounds(61, 39, 114, 22);
		contentPanel.add(cmbSexo);
		
		JLabel lblStatus = new JLabel("Status");
		lblStatus.setBounds(10, 100, 46, 14);
		contentPanel.add(lblStatus);
		
		cmbStatus = new JComboBox<String>();
		cmbStatus.setModel(new DefaultComboBoxModel(new String[] {"Ativo", "Inativo"}));
		cmbStatus.setBounds(61, 96, 114, 22);
		contentPanel.add(cmbStatus);
		
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		
		MaskFormatter Data = null;
		try {
			Data = new MaskFormatter("##/##/####");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Data.setPlaceholderCharacter('_');  
		
		txtNasc = new JFormattedTextField(sdf);
		txtNasc.setBounds(61, 68, 119, 20);
		contentPanel.add(txtNasc);
		
		JLabel lblNasc = new JLabel("Dt Nasc.");
		lblNasc.setBounds(10, 71, 46, 14);
		contentPanel.add(lblNasc);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				btnSalvar = new JButton("Salvar");
				btnSalvar.setActionCommand("Salvar");
				buttonPane.add(btnSalvar);
				getRootPane().setDefaultButton(btnSalvar);
			}
			{
				btnFechar = new JButton("Fechar");
				btnFechar.setActionCommand("Fechar");
				buttonPane.add(btnFechar);
			}
		}
	}

	@Override
	public void registerListener(Object t) {
		CtrlPaciente control = (CtrlPaciente) t;
		btnSalvar.addActionListener(control);
		btnFechar.addActionListener(control);
	}

	@Override
	public void populaCampos(Object t) {
		Paciente pac = (Paciente) t;
		txtNome.setText(pac.getNome());
		cmbSexo.setSelectedIndex(pac.getSexo()=="M"?1:0);
		
		Date data = null;
		try {
			data = (Date) new SimpleDateFormat("dd/mm/yyyy").parse(pac.getDataNascimento().toString());
		} catch (ParseException e) {
			e.printStackTrace();
			System.out.println("Erro ao converter data no cadastro do paciente");
		}
		
		txtNasc.setText(data.toString());
		cmbStatus.setSelectedIndex(pac.getStatus()==0?1:0);
	}

	@Override
	public Object populaObjeto(Object t) {
		Paciente pac = (Paciente) t;
		pac.setNome(txtNome.getText());
		pac.setSexo(cmbSexo.getSelectedIndex()==0?"M":"F");
		
		Date data = null;
		try {
			data = (Date) new SimpleDateFormat("yyyy-mm-dd").parse(txtNasc.getText());
		} catch (ParseException e) {
			e.printStackTrace();
			System.out.println("Erro ao converter data no cadastro do paciente");
		}  
		
		pac.setDataNascimento(data);
		pac.setStatus(cmbStatus.getSelectedIndex()==0?1:0);
		
		return pac;
	}

	@Override
	public void liberaCampos(boolean libera) {
		btnSalvar.setVisible(libera);
		txtNome.setEditable(libera);
		txtNasc.setEditable(libera);
	}

	@Override
	public void limpaCampos() {
		txtNome.setText(null);
		cmbSexo.setSelectedIndex(0);
		txtNasc.setText(null);
		cmbStatus.setSelectedIndex(0);		
	}

	@Override
	public boolean validaCampos() {
		if (txtNome.getText()=="" || txtNasc.getText()=="")
			return true;
		return false;
	}
}
